package com.captain.special.二叉树.简单;

import com.captain.leetcode.TreeNode;

/**
 * Des:
 *
 * @author XL
 * @Date 2022/5/9 11:42
 */
public class 单值二叉树965 {
    boolean flag;
    int pre;

    public boolean isUnivalTree(TreeNode root) {
        pre = -1;
        recur(root);
        return !flag;
    }

    public void recur(TreeNode node) {
        if (node == null) return;
        int val = node.val;
        if (pre == -1) {
            pre = val;
        } else {
            if (flag) {
                return;
            }
            //不相等
            if (val != pre) {
                flag = true;
            }
        }
        recur(node.left);
        recur(node.right);
    }
}
